package cn.bugstack.gateway.executor;

import cn.bugstack.gateway.datasource.Connection;
import cn.bugstack.gateway.executor.result.GatewayResult;
import cn.bugstack.gateway.mapping.HttpStatement;
import cn.bugstack.gateway.session.Configuration;
import cn.bugstack.gateway.type.SimpleTypeRegistry;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public abstract class BaseExecutor implements Executor {
    private Logger logger = LoggerFactory.getLogger(BaseExecutor.class);

    protected Configuration configuration;
    protected Connection connection;

    public BaseExecutor(Configuration configuration, Connection connection) {
        this.configuration = configuration;
        this.connection = connection;
    }

    public GatewayResult exec(HttpStatement httpStatement, Map<String,Object> params) throws Exception{
        //参数处理：后续的一些参数检验也可以在这里封装
        String methodName = httpStatement.getMethodName();
        String parameterType = httpStatement.getParameterType();
        String[] parameterTypes = new String[]{parameterType};
        Object[] args = SimpleTypeRegistry.isSimpleType(parameterType)?params.values().toArray():new Object[]{params};
        logger.info("执行调用 method：{}#{}.{}({}) args：{}", httpStatement.getApplication(), httpStatement.getInterfaceName(), httpStatement.getMethodName(), JSON.toJSONString(parameterTypes), JSON.toJSONString(args));
        //抽象方法
        try{
            Object data = doExe(methodName,parameterTypes,args);
            return GatewayResult.buildError(data);
        }catch (Exception e){
            return GatewayResult.buildError(e.getMessage());
        }
    }

    protected abstract Object doExe(String methodName, String[] parameterTypes, Object[] args);

}
